package com.potato.rxjavademo

import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.potato.rxjavademo.rxview.RxView
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit


class MainActivity : AppCompatActivity() {

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    @SuppressLint("CheckResult")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        imageView=findViewById(R.id.imageView)
        //rxjava简单用法


//        Observable.create(ObservableOnSubscribe<String> { e->
//            run {
//                e.onNext("胖子")
//            }
//        }).map(Function {
//            kotlin.run { return@Function }
//        }).subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe { s->
//                run {
//                    Log.e("TAG", "s=$s")
//                }
//            }

//       var observable =object: Function<String?, ObservableSource<String?>?> {
//            @Throws(Exception::class)
//           override fun apply(t: String): ObservableSource<String?>? {
//                return null
//           }
//       };
//        Observable.create<String> {
//                emitter ->
//            run {
//                emitter.onNext("胖子")
//            }
//        }.subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .doOnNext {
//                run{
//                    Log.e("TAG","s$it")
//                }
//            }.observeOn(Schedulers.io())
//            .flatMap(observable)
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe {
//                run {
//                    Log.e("TAG","s$it")
//                }
//               }

//        test()

        //自定义RxJava操作符
        RxView.clicks(imageView!!)
            .throttleFirst(2000, TimeUnit.MILLISECONDS) // 2秒钟之内 响应你一次
            .flatMap { Observable.just("张三") }
            .observeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread()) // 给下面切换 主线程
            .subscribe(Consumer<Any> { name -> // 如果我要更新UI  会报错2  不会报错1
                Log.d("TAG", "name=: $name")
            })
    }

    var drawableRes = R.drawable.ic_launcher_background
    var imageView: ImageView? = null

    @SuppressLint("CheckResult")
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    fun test(){
        Observable.create(ObservableOnSubscribe<Drawable> { e->
            run {
                val drawable = theme.getDrawable(drawableRes)
                e.onNext(drawable)
            }
        }).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(Consumer { s->  run {
            imageView!!.setImageDrawable(s)
        }})
    }
}

